package modeller.queueingnetworksmodel.probabilities;

import java.util.Arrays;

/**
 * Created by:  Jason Ye
 * Date:        30/05/2012
 * Time:        12:33
 */
public class TransactionRoutingAlgoState {

    private int currentTable;
    private int[] branch;
    private int bran_table;
    private int[] prev_branch;
    private double[][] p;
    private boolean isBranchNil;

    private static final int nil = 0;
    private boolean isPrevBranchNil;

    public TransactionRoutingAlgoState(int currentTable, int[] branch, int bran_table, int[] prev_branch, double[][] p) {
        this.currentTable = currentTable;
        this.branch = branch;
        this.bran_table = bran_table;
        this.prev_branch = prev_branch;
        this.p = p;

        this.isBranchNil = true;
        this.isPrevBranchNil = true;
    }

    public int getCurrentTable() {
        return currentTable;
    }

    public void setCurrentTable(int currentTable) {
        this.currentTable = currentTable;
    }

    public int[] getBranch() {
        return branch;
    }

    public void setTrueBranch(int r) {
        branch[1] = r;
        isBranchNil = false;
    }

    public void setFalseBranch(int r) {
        branch[0] = r;
        isBranchNil = false;
    }

    public int getBran_table() {
        return bran_table;
    }

    public void setBran_table(int bran_table) {
        this.bran_table = bran_table;
    }

    public int[] getPrev_branch() {
        return prev_branch;
    }

    public void setPrev_branch(int[] prev_branch) {
        this.prev_branch = prev_branch;
    }

    public double[][] getP() {
        return p;
    }

    public double getP(int currentTable, int destTable) {
        return p[currentTable][destTable];
    }

    public void setP(int currentTable, int destTable, double value) {

        p[currentTable][destTable] = value;

    }

    public boolean isBranchNil() {
        return isBranchNil;
    }

    public void connectLastTableFromBranchToThisRelation(int r) {

        // set all branches' next state to that of relation r
        for (int j = 0; j < branch.length; j++) {
            p[branch[j]][r] = 1;
        }
    }

    public void setBranchToNil() {

        for (int i = 0; i < branch.length; i++) {
            branch[i] = nil;
        }
        isBranchNil = true;
    }

    public void setPrevBranchToCurrentBranch() {

        prev_branch = Arrays.copyOf(branch, branch.length);
        isPrevBranchNil = isBranchNil;
    }

    public void setPrevBranchToNil() {

        for (int i = 0; i < prev_branch.length; i++) {
            prev_branch[i] = nil;
        }
        isPrevBranchNil = true;
    }

    public void setBranTableToCurrentTable() {

        bran_table = currentTable;
    }

    public boolean isPrevBranchNil() {

        return isPrevBranchNil;
    }

    public void connectLastTableAccessedByPrevBranchToThisSQL(int r, double pi) {

        for (int j = 0; j < prev_branch.length; j++) {
            p[prev_branch[j]][r] = pi;
        }


    }

    public void terminate() {
        // if branch[]!=nil then
        if (!isBranchNil) {
            // for all branch
            //P[c,branch[i],0] = 1
            connectLastTableFromBranchToThisRelation(0);
            // end for
        } else {
            // P [c, current_table,0] = 1
            p[currentTable][0]=1;
        } //end if
    }
}

